﻿//======================================================================
//
//	XNA ContentBrowser
//
//  by Matt Pettineo (mpettineo@gmail.com)
//  05/03/09  
//
//======================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;
using System.Drawing.Design;

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

using ContentBrowser;

namespace ContentBrowserSample
{
    /// <summary>
    /// A simple class for setting up the XNA GraphicsDevice and ContentManager,
    /// loading some basic content, and drawing it to the screen.
    /// </summary>
    public class Project
    {
        private GraphicsDeviceService graphicsDeviceService;
        private GraphicsDevice graphicsDevice;
        private ServiceCollection serviceCollection;
        private ContentManager contentManager;
        private SpriteBatch spriteBatch;
        float modelRotation = 0;
        
        private Model model;
        private Texture2D texture;
        private SpriteFont font;

        private string modelName = string.Empty;
        private string textureName = string.Empty;
        private string fontName = string.Empty;


        [Browsable(false)]
        public ServiceCollection ServiceCollection
        {
            get { return serviceCollection; }
        }

        [Browsable(false)]
        public GraphicsDeviceService GraphicsDeviceService
        {
            get { return graphicsDeviceService; }
        }

        [Browsable(true)]
        [Category("Settings")]
        [Description("The name of the SpriteFont to use for text rendering")]
        [Editor(typeof(ContentBrowserEditor<SpriteFont>), typeof(UITypeEditor))]
        public string FontName
        {
            get { return fontName; }
            set
            {
                fontName = value;
                if (!string.IsNullOrEmpty(fontName))
                {
                    if (contentManager != null)
                        font = contentManager.Load<SpriteFont>(fontName);
                    else
                        font = null;
                }
                else
                    font = null;
            }
        }

        [Browsable(true)]
        [Category("Settings")]
        [Description("The name of the Model to draw")]
        [Editor(typeof(ContentBrowserEditor<Model>), typeof(UITypeEditor))]
        public string ModelName
        {
            get { return modelName; }
            set
            {
                modelName = value;
                if (!string.IsNullOrEmpty(modelName))
                {
                    if (contentManager != null)
                        model = contentManager.Load<Model>(modelName);
                    else
                        model = null;
                }
                else
                    model = null;
            }
        }

        [Browsable(true)]
        [Category("Settings")]
        [Description("The name of the Texture to draw as a sprite")]
        [Editor(typeof(ContentBrowserEditor<Texture2D>), typeof(UITypeEditor))]
        public string TextureName
        {
            get { return textureName; }
            set
            {
                textureName = value;
                if (!string.IsNullOrEmpty(textureName))
                {
                    if (contentManager != null)
                        texture = contentManager.Load<Texture2D>(textureName);
                    else
                        texture = null;
                }
                else
                    texture = null;
            }
        }

        public Project(IntPtr windowHandle)
        {
            graphicsDeviceService = new GraphicsDeviceService(windowHandle);
            graphicsDevice = graphicsDeviceService.GraphicsDevice;
            serviceCollection = new ServiceCollection();
            serviceCollection.AddService<IGraphicsDeviceService>(graphicsDeviceService);
            contentManager = new ContentManager(serviceCollection, "Content");
            spriteBatch = new SpriteBatch(graphicsDevice);

            TextureName = "Textures\\ShipSprite";
            ModelName = "Models\\Teapot";
            FontName = "Fonts\\Arial";
        }      

        public void Render(float dt)
        {
            graphicsDevice.Clear(Color.DarkBlue);
            
            if (model != null)
            {
                graphicsDevice.RenderState.DepthBufferEnable = true;
                graphicsDevice.RenderState.DepthBufferWriteEnable = true;

                Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, 10), Vector3.Zero, Vector3.Up);
                Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, graphicsDevice.Viewport.AspectRatio, 1, 100);
                
                modelRotation += dt;
                Matrix rotation = Matrix.CreateRotationY(MathHelper.WrapAngle(modelRotation));

                foreach(ModelMesh mesh in model.Meshes)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        effect.View = view;
                        effect.Projection = projection;
                        effect.LightingEnabled = true;
                        effect.PreferPerPixelLighting = true;
                        effect.EnableDefaultLighting();
                        effect.World = rotation;                        
                    }

                    mesh.Draw();
                }                
            }

            spriteBatch.Begin();

            if (texture != null)            
                spriteBatch.Draw(texture, new Vector2(50, 50), Color.White);            

            if (font != null)
            {
                Vector2 drawPos = new Vector2(50, graphicsDevice.Viewport.Height - 50);
                spriteBatch.DrawString(font, "XNA ContentBrowser Rox!!", drawPos, Color.White);
            }

            spriteBatch.End();

        }

    }
}